package dao.impl;

import dao.StudentDao;
import generator.domain.Student;
import org.apache.ibatis.session.SqlSession;
import util.MybatisUtil;

import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private SqlSession sqlSession = null;
    @Override
    public int insert(Student student) {
        sqlSession = MybatisUtil.getSqlSession();
        String sqlId = "dao.StudentDao.insert";
        int rows = sqlSession.insert(sqlId, student);
        sqlSession.commit();
        sqlSession.close();
        return rows;
    }

    @Override
    public List<Student> selectAll() {
        sqlSession = MybatisUtil.getSqlSession();
        String sqlId = "dao.StudentDao.selectAll";
        List<Student> list = sqlSession.selectList(sqlId);
        sqlSession.close();
        return list;
    }

    @Override
    public Student selectById() {
        return null;
    }

    @Override
    public Student selectByName(String name) {
        sqlSession = MybatisUtil.getSqlSession();
        String sqlId = "dao.StudentDao.selectByName";
        Student student = sqlSession.selectOne(sqlId,name);
        return student;
    }

    @Override
    public int update(Student student) {
        sqlSession = MybatisUtil.getSqlSession();
        String sqlId = "dao.StudentDao.update";
        int rows = sqlSession.update(sqlId, student);
        sqlSession.commit();
        sqlSession.close();
        return rows;
    }

    @Override
    public int delete(int id) {
        sqlSession = MybatisUtil.getSqlSession();
        String sqlId = "dao.StudentDao.delete";
        int row = sqlSession.delete(sqlId,id);
        sqlSession.commit();
        sqlSession.close();
        return row;
    }
}
